package com.gxg.demo10.mydemo11.stackbarchart;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.StackedValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.gxg.demo10.mydemo11.R;
import com.gxg.demo10.mydemo11.linechart.XYMarkerView;
import com.socks.library.KLog;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class StackBarChartActivity extends AppCompatActivity implements OnChartValueSelectedListener {

    @Bind(R.id.chart)
    BarChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stack_bar_chart);
        ButterKnife.bind(this);

        chart.setOnChartValueSelectedListener(this);

        chart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        chart.setMaxVisibleValueCount(40);

        // scaling can now only be done on x- and y-axis separately
        chart.setPinchZoom(false);

        chart.setDrawGridBackground(false);
        chart.setDrawBarShadow(false);

        chart.setDrawValueAboveBar(false);
        chart.setHighlightFullBarEnabled(false);

        // change the position of the y-labels
        YAxis leftAxis = chart.getAxisLeft();
//        leftAxis.setValueFormatter(new MyValueFormatter("K"));
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        chart.getAxisRight().setEnabled(false);

        XAxis xLabels = chart.getXAxis();
        xLabels.setPosition(XAxis.XAxisPosition.TOP);

        // chart.setDrawXLabels(false);
        // chart.setDrawYLabels(false);

        Legend l = chart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setFormSize(8f);
        l.setFormToTextSpace(4f);
        l.setXEntrySpace(6f);

        initData();
    }

    private void initData() {
        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < mvalues.length; i++) {
            int mul = (mvalues.length + 1);
            int val1 = (int) (Math.random() * mul) + mul / 3;
            int val2 = (int) (Math.random() * mul) + mul / 3;
            int val3 = (int) (Math.random() * mul) + mul / 3;
            int val4 = (int) (Math.random() * mul) + mul / 3;
            int val5 = (int) (Math.random() * mul) + mul / 3;

            values.add(new BarEntry(
                    i,
                    new float[]{val1, val2, val3,val4,val5}));
        }

        BarDataSet set1;

        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(values, "");
            set1.setDrawIcons(false);
            set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
            set1.setStackLabels(titles);
            set1.setDrawValues(true);
            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setBarWidth(0.4f);
            data.setValueFormatter(new StackedValueFormatter(false, "", 1));
            data.setValueTextColor(Color.WHITE);

            chart.setData(data);
        }
        MyXFormatter formatter = new MyXFormatter(mvalues);
        XAxis xAxis = chart.getXAxis();
        xAxis.setGranularity(1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(true);
        chart.setVisibleXRangeMaximum(12);
        //设置悬浮
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLimitLinesBehindData(true);
        //显示个数
        xAxis.setLabelCount(12);
        xAxis.setValueFormatter(formatter);
        XYMarkerView mv = new XYMarkerView(this, formatter);
        mv.setChartView(chart);
        chart.setMarker(mv);
        chart.setFitBars(true);

        chart.invalidate();
    }

    public String[] titles = {"执法一队",
            "执法二队",
            "执法三队",
            "执法四队",
            "执法五队",
    };

    public String[] mvalues = {"一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
    };

    public class MyXFormatter extends ValueFormatter {

        private String[] mValues;

        public MyXFormatter(String[] values) {
            this.mValues = values;
        }

        private static final String TAG = "MyXFormatter";

        @Override
        public String getFormattedValue(float value) {
            if (value >= 0) {
                return mValues[(int) value % mValues.length];
            } else {
                return "";
            }

        }
    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {
        KLog.e("sss  "+e.getX());
        KLog.e("sss  "+e.getY());
        KLog.e("sss  "+e.getData());
    }

    @Override
    public void onNothingSelected() {

    }
}
